package mpYoga

import (
	"duoke/context"
	"duoke/model"
	"duoke/model/yogaModel"
	"duoke/service"
	yogaService "duoke/service/yoga"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"duoke/utils/yaml"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var Order = new(orderCtl)

type orderCtl struct{}

func (ctl *orderCtl) CreateOrder(c *gin.Context) {
	var req yogaModel.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	order, err := yogaService.Order.CreateOrder(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", gin.H{"order_no": order})
}
func (ctl *orderCtl) OrderInfo(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	info, err := yogaService.Order.OrderInfo(c, orderNo)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", info)
}
func (ctl *orderCtl) PayOrder(c *gin.Context) {
	var req yogaModel.PayOrder
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	// 创建订单
	order, err := yogaService.Order.OrderAmount(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	config := yaml.GetConfig()
	if config.SubMch.SubType == "2" {
		order.SubMchId = ""
	}
	// 构建支付单
	notifyUrl := context.Global.Domain + "/mp/notify/yoga_notify"
	pay, err := service.WechatPay.OrderPay(c, &model.ReqWechatPay{
		NotifyUrl: notifyUrl,
		Openid:    order.Openid,
		OrderNo:   order.OrderNo,
		PayAmount: order.TotalAmount,
		Desc:      order.Body,
		SubMchId:  order.SubMchId,
	})
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", pay)
}
func (ctl *orderCtl) MyOrder(c *gin.Context) {
	var req yogaModel.ReqOrderList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	list, total, err := yogaService.Order.MyOrder(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", gin.H{"list": list, "total": total})
}
func (ctl *orderCtl) Reservation(c *gin.Context) {
	var req yogaModel.ReqReservation
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	if err := yogaService.Order.Reservation(c, &req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", nil)
}
func (ctl *orderCtl) OrderList(c *gin.Context) {
	var req model.ReqListV2
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	list, total, err := yogaService.Order.OrderList(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", gin.H{"list": list, "total": total})
}
func (ctl *orderCtl) OrderDetail(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	info, err := yogaService.Order.OrderDetail(c, orderNo)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "成功", info)
}
